package cn.kxlove.zhbj.pager.impl.menu;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.viewpagerindicator.TabPageIndicator;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

import cn.kxlove.zhbj.MainActivity;
import cn.kxlove.zhbj.R;
import cn.kxlove.zhbj.domain.respone.NewsMenuBean;
import cn.kxlove.zhbj.pager.BaseMenuDetailPager;
import cn.kxlove.zhbj.pager.impl.TabDetailPager;


public class NewsMenuDetailPager extends BaseMenuDetailPager {

    @ViewInject(R.id.vp_new_detail)
    private ViewPager mViewPager;

    @ViewInject(R.id.indicator)
    private TabPageIndicator mIndicator;

    private ArrayList<NewsMenuBean.NewsTabData> mTabList;
    private ArrayList<TabDetailPager> mPagers;

    public NewsMenuDetailPager(Activity activity, ArrayList<NewsMenuBean.NewsTabData> children) {
        super(activity);
        mTabList = children;
    }

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.pager_new_menu_detail,null);
        x.view().inject(this,view);

        return view;
    }

    @Override
    public void initData() {
        mPagers = new ArrayList<>();
        super.initData();
        for (int i = 0; i < mTabList.size(); i++) {
            TabDetailPager pager = new TabDetailPager(mActivity,mTabList.get(i));
            mPagers.add(pager);
        }
        mViewPager.setAdapter(new NewsMenuAdapter());

        mIndicator.setViewPager(mViewPager);
        mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    // 打开SlidingMenu
                    setSlidingMenuEnable(true);
                } else {
                    // 禁用SlidingMenu
                    setSlidingMenuEnable(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Event(R.id.btn_next_page)
    private void nextPage(View view) {
        int currentItem = mViewPager.getCurrentItem();
        mViewPager.setCurrentItem(++currentItem);
    }

    /**
     * 设置侧边栏可用或不可用
     *
     * @param enable
     */
    protected void setSlidingMenuEnable(boolean enable) {
        MainActivity mainUI = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();
        if (enable) {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        } else {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    class NewsMenuAdapter extends PagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabList.get(position).title;
        }

        @Override
        public int getCount() {
            return mTabList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TabDetailPager pager = mPagers.get(position);
            View view = pager.mRootView;
            pager.initData();
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
